/**
 * 
 */
package cn.maszy.service.impl.contract;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.maszy.common.Pager;
import cn.maszy.common.SessionFactory;
import cn.maszy.dao.contract.B_T_Ufp_ContractMapper;
import cn.maszy.dao.contract.B_T_Ufp_PaymentMapper;
import cn.maszy.model.contract.B_T_Ufp_Contract;
import cn.maszy.model.contract.B_T_Ufp_ContractExample;
import cn.maszy.model.contract.B_T_Ufp_ContractExample.Criteria;
import cn.maszy.model.contract.B_T_Ufp_Payment;
import cn.maszy.model.contract.B_T_Ufp_PaymentExample;
import cn.maszy.service.contract.B_T_Ufp_ContractService;


@Service
public class B_T_Ufp_ContractServiceImpl implements B_T_Ufp_ContractService{
	@Autowired
	private B_T_Ufp_ContractMapper b_T_Ufp_ContractMapper;
	@Autowired
	private B_T_Ufp_PaymentMapper b_T_Ufp_PaymentMapper;
	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_ContractService#update(cn.maszy.model.investment.B_T_Ufp_Contract, cn.maszy.model.investment.B_T_Ufp_ContractExample)
	 */
	public int update(B_T_Ufp_Contract b_T_Ufp_Contract ,B_T_Ufp_Payment b_T_Ufp_Payment,String flag) {
		// TODO Auto-generated method stub
		if(flag=="1"||"1".equals(flag)){
			 b_T_Ufp_Contract.setIsDelete(0);
			 b_T_Ufp_Contract.setCreateTime(new Date());
			 b_T_Ufp_Contract.setCreateUser(SessionFactory.getSessionUserRealNam());
			 String ss[]=b_T_Ufp_Payment.getKtimec().split(",");
			 String aa[]=b_T_Ufp_Payment.getEtimeb().split(",");
			 String bb[]=b_T_Ufp_Payment.getPaymentionz().split(",");
			 String cc[]=b_T_Ufp_Payment.getConditionk().split(",");
			 	for (int i = 0; i < ss.length; i++) {
			 	 B_T_Ufp_Payment record = new B_T_Ufp_Payment();
			 	 	record.setUuid(UUID.randomUUID().toString());
			 	 	record.setSerialnumber(b_T_Ufp_Contract.getUuid());
			 	 	record.setKtimec(ss[i]);
			 	 	record.setEtimeb(aa[i]);
			 	 	record.setPaymentionz(bb[i]);
			 	 	record.setConditionk(cc[i]);
					b_T_Ufp_PaymentMapper.insert(record);
				}
			 
			 return  b_T_Ufp_ContractMapper.insert(b_T_Ufp_Contract);
		 }else if(!"".equals(b_T_Ufp_Payment.getUuid())){
			 	 String ee[]=b_T_Ufp_Payment.getUuid().split(",");//这里切割的时候会有空格出现,所以下面有使用空格的判断
				 String ss[]=b_T_Ufp_Payment.getKtimec().split(",");
				 String aa[]=b_T_Ufp_Payment.getEtimeb().split(",");
				 String bb[]=b_T_Ufp_Payment.getPaymentionz().split(",");
				 String cc[]=b_T_Ufp_Payment.getConditionk().split(",");
				 //当修改付款方式中具体那一条数据时,ee数组中会多产生一个空值,要去掉,所以用到下面 数组转换成list,再由list转换成数组,放置数组越界
				 List<String> list=new ArrayList<String>();
					 if(ee.length > ss.length){
						 for(int j = 0; j < ee.length; j++){
							 if(!"".equals(ee[j])){
								 list.add(ee[j]);
							 }
						 }
					 }
					 int size = list.size();
					String arrayEe[] =(String[])list.toArray(new String[size]);
				 	for (int i = 0; i < ss.length; i++) {
				 	 B_T_Ufp_Payment record = new B_T_Ufp_Payment();
				 	 //用于修改合同付款方式
				 	 if(ee.length > ss.length){
				 		 if(!"".equals(arrayEe[i])){
				 			record.setUuid(arrayEe[i]);
					 	 	record.setSerialnumber(b_T_Ufp_Contract.getUuid());
					 	 	record.setKtimec(ss[i]);
					 	 	record.setEtimeb(aa[i]);
					 	 	record.setPaymentionz(bb[i]);
					 	 	record.setConditionk(cc[i]);
					 	 	b_T_Ufp_PaymentMapper.updateByPrimaryKeySelective(record);
				 		 }
				 	 }else{
				 		//这里有空格的判断
				 	 	//用于修改个合同,新建付款方式
				 		 if("".equals(ee[i]) || " ".equals(ee[i])){
						 	 	record.setUuid(UUID.randomUUID().toString());
						 	 	record.setSerialnumber(b_T_Ufp_Contract.getUuid());
						 	 	record.setKtimec(ss[i]);
						 	 	record.setEtimeb(aa[i]);
						 	 	record.setPaymentionz(bb[i]);
						 	 	record.setConditionk(cc[i]);
						 	 	b_T_Ufp_PaymentMapper.insert(record);
						 	 }else{
						 		record.setUuid(ee[i]);
						 	 	record.setSerialnumber(b_T_Ufp_Contract.getUuid());
						 	 	record.setKtimec(ss[i]);
						 	 	record.setEtimeb(aa[i]);
						 	 	record.setPaymentionz(bb[i]);
						 	 	record.setConditionk(cc[i]);
						 	 	b_T_Ufp_PaymentMapper.updateByPrimaryKeySelective(record);
						 	 }
				 		}
				 	}
				
			 }
			
			 b_T_Ufp_Contract.setUpdateTime(new Date());
			 b_T_Ufp_Contract.setUpdateUser(SessionFactory.getSessionUserRealNam());
			 return b_T_Ufp_ContractMapper.updateByPrimaryKeySelective(b_T_Ufp_Contract);
			
		 }


	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_ContractService#selectOne(java.lang.String)
	 */
	public B_T_Ufp_Contract selectOne(String uuid) {
		// TODO Auto-generated method stub
		B_T_Ufp_Contract record=b_T_Ufp_ContractMapper.selectByPrimaryKey(uuid);
		if("".equals(uuid)){
			record = new B_T_Ufp_Contract();
		}
		return record;
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_ContractService#delete(java.lang.String)
	 */
	public int delete(String checkIds) {
		// TODO Auto-generated method stub
		for (String id : checkIds.split(", ")) {
			b_T_Ufp_ContractMapper.deleteByPrimaryKey(id);
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_ContractService#count(cn.maszy.model.investment.B_T_Ufp_ContractExample)
	 */
	public int count(B_T_Ufp_ContractExample example,B_T_Ufp_ContractMapper b_T_Ufp_ContractMapper) {
		// TODO Auto-generated method stub
		return b_T_Ufp_ContractMapper.countByExample(example);
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_ContractService#selectList(cn.maszy.model.investment.B_T_Ufp_Contract)
	 */
	public Pager<B_T_Ufp_Contract> selectList(B_T_Ufp_Contract b_T_Ufp_Contract,Pager<B_T_Ufp_Contract> pager) {
		// TODO Auto-generated method stub
		// 添加查询条件
		B_T_Ufp_ContractExample example=new B_T_Ufp_ContractExample();
		Criteria criteria = example.createCriteria();
		if(b_T_Ufp_Contract!=null){
			if(b_T_Ufp_Contract.getContractname()!=null&&!"".equals(b_T_Ufp_Contract.getContractname())){
				criteria.andContractnameLike("%"+b_T_Ufp_Contract.getContractname()+"%");
			}
		}else{
			criteria.andUuidIsNotNull();
		}
		example.or(criteria);
		int totalCount=new B_T_Ufp_ContractServiceImpl().count(example,b_T_Ufp_ContractMapper);
		pager.setTotalRecords(totalCount);
		example.setLimitStart(pager.getStartRecord());
		example.setLimitEnd(pager.getPageSize());
		example.setOrderByClause(pager.getOrderField()+" "+pager.getOrderDirection());
		
		// 取得总的记录条数
		List<B_T_Ufp_Contract>  list=b_T_Ufp_ContractMapper.selectByExample(example);
		pager.setPageRecords(list);
		return pager;
	}

	public List<B_T_Ufp_Payment> seelctlistpay(	List<B_T_Ufp_Payment> b_T_Ufp_Payment) {
		B_T_Ufp_PaymentExample example=new B_T_Ufp_PaymentExample();
		b_T_Ufp_Payment=b_T_Ufp_PaymentMapper.selectByExample(example);
		return b_T_Ufp_Payment;
	}

	public List<B_T_Ufp_Payment> selectOnepay(String uuid) {
		B_T_Ufp_PaymentExample example=new B_T_Ufp_PaymentExample();
		cn.maszy.model.contract.B_T_Ufp_PaymentExample.Criteria criteria = example.createCriteria();
		criteria.andSerialnumberEqualTo(uuid);
		example.or(criteria);
		List<B_T_Ufp_Payment> b_T_Ufp_Payment= (List<B_T_Ufp_Payment>) b_T_Ufp_PaymentMapper.selectByExample(example);
		return  b_T_Ufp_Payment;
	}
}
